/*
 * Copyright 1999-2012 Alibaba Group.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.zhulu.dubbo.group;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.examples.version.api.VersionService;
import com.alibaba.fastjson.JSON;
import me.zhulu.dubbo.group.api.GroupService;
import me.zhulu.dubbo.group.impl.DubboFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * GroupConsumer
 * 
 * @author william.liangf
 */
public class GroupConsumerApi {

    public static void main(String[] args) throws Exception {
        String config = GroupConsumerApi.class.getPackage().getName().replace('.', '/') + "/group-consumer-api.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        context.start();

        DubboFactory groupConsumerApi = (DubboFactory) context.getBean("dubboFactory");

        GroupService groupService1 = groupConsumerApi.get("provider1", GroupService.class); // 注意：此代理对象内部封装了所有通讯细节，对象较重，请缓存复用
        GroupService groupService2 = groupConsumerApi.get("provider2", GroupService.class); // 注意：此代理对象内部封装了所有通讯细节，对象较重，请缓存复用

        String hello1 = groupService1.sayHello("周雄海");
        String hello2 = groupService2.sayHello("竹路");

        System.out.println(hello1);
        System.out.println(hello2);

        VersionService versionService = (VersionService) context.getBean("versionService");
        String version = versionService.sayHello("version");
        System.out.println(version);
    }

//    public static void main(String[] args) throws Exception {
//        String config = GroupConsumerApi.class.getPackage().getName().replace('.', '/') + "/group-consumer-api.xml";
//        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
//        context.start();
//
//        String[] applicationName = context.getBeanNamesForType(ApplicationConfig.class);
//        System.out.println(JSON.toJSON(applicationName));
//        ApplicationConfig application = (ApplicationConfig) context.getBean(applicationName[0]);
//
//        String[] registerName = context.getBeanNamesForType(RegistryConfig.class);
//        System.out.println(JSON.toJSON(registerName));
//        RegistryConfig registry = (RegistryConfig) context.getBean(registerName[0]);
//
//        // 引用远程服务
//        ReferenceConfig<GroupService> reference = new ReferenceConfig<GroupService>(); // 此实例很重，封装了与注册中心的连接以及与提供者的连接，请自行缓存，否则可能造成内存和连接泄漏
//        reference.setApplication(application);
//        reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
//        reference.setInterface(GroupService.class);
//        reference.setGroup("provider1");
////        reference.setVersion("1.0.0");
////        reference.setGroup("finance"); //服务分组，当一个接口有多个实现，可以用分组区分，必需和服务提供方一致
//
//// 和本地bean一样使用xxxService
//        GroupService groupService = reference.get(); // 注意：此代理对象内部封装了所有通讯细节，对象较重，请缓存复用
//
//
//        // 引用远程服务
//        ReferenceConfig<GroupService> reference2 = new ReferenceConfig<GroupService>(); // 此实例很重，封装了与注册中心的连接以及与提供者的连接，请自行缓存，否则可能造成内存和连接泄漏
//        reference2.setApplication(application);
//        reference2.setRegistry(registry); // 多个注册中心可以用setRegistries()
//        reference2.setInterface(GroupService.class);
//        reference2.setGroup("provider2");
////        reference.setVersion("1.0.0");
////        reference.setGroup("finance"); //服务分组，当一个接口有多个实现，可以用分组区分，必需和服务提供方一致
//
//// 和本地bean一样使用xxxService
//        GroupService groupService2 = reference2.get(); // 注意：此代理对象内部封装了所有通讯细节，对象较重，请缓存复用
//
//        String hello1 = groupService.sayHello("周雄海");
//        String hello2 = groupService2.sayHello("竹路");
//
//        System.out.println(hello1);
//        System.out.println(hello2);
//    }

}
